package cn.rongdao.jrkabao.ui.main.mainfragment.cashadvance.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.bean.cashadvance.HomeBean;
import cn.rongdao.jrkabao.bean.main.banner.BannerBean;
import cn.rongdao.jrkabao.ui.main.mainfragment.cashadvance.ui.BannerComponent;
import cn.rongdao.jrkabao.ui.main.mainfragment.cashadvance.ui.CashHeadComponent;
import cn.rongdao.jrkabao.ui.main.mainfragment.cashadvance.ui.HomeIdentityUIComponent;
import cn.rongdao.jrkabao.ui.main.mainfragment.cashadvance.ui.LoanAndAdvComponent;
import cn.rongdao.jrkabao.ui.main.mainfragment.cashadvance.ui.NavigationComponent;
import cn.rongdao.jrkabao.ui.main.mainfragment.cashadvance.viewholder.CashAdvanceViewHolder;

/**
 * Created by WANG on 17/12/18
 */

public class CashAdvanceAdapter extends RecyclerView.Adapter<CashAdvanceViewHolder> {
    public final static int VIEWTYPE_HEAD = 1;
    public final static int VIEWTYPE_NAVIGATION = 2;
    public final static int VIEWTYPE_BANNER = 3;
    public final static int VIEWTYPE_IDENTITY = 4;
    public final static int VIEWTYPE_LOAN = 5;
    public final static int VIEWTYPE_ADV = 6;

    public final static String HEAD = "head";
    public final static String NAVIGATION = "navigation";
    public final static String BANNER = "banner";
    public final static String IDENTITY = "identity";
    public final static String LOAN = "loan";
    public final static String ADV = "adv";


    private LayoutInflater layoutInflater;
    private List<String> data;

    private HomeBean homeBean;
    private List<BannerBean> bannerData;
    private HomeIdentityUIComponent.OnHomeLivenessListener onHomeLivenessListener;

    public CashAdvanceAdapter(Context context) {
        if (data == null) {
            data = new ArrayList<>();
        }
        layoutInflater = LayoutInflater.from(context);
    }

    public void setRefreshData(List<String> list) {
        data.clear();
        data.addAll(list);
        notifyDataSetChanged();
    }

    public void setBannerData(List<BannerBean> bannerData) {
        this.bannerData = bannerData;
    }

    public void setHomeBean(HomeBean homeBean) {
        this.homeBean = homeBean;
    }

    @Override
    public int getItemViewType(int position) {
        if (data == null || data.size() == 0) {
            return -1;
        }
        String key = data.get(position);
        switch (key) {
            case HEAD:
                return VIEWTYPE_HEAD;
            case NAVIGATION:
                return VIEWTYPE_NAVIGATION;
            case BANNER:
                return VIEWTYPE_BANNER;
            case IDENTITY:
                return VIEWTYPE_IDENTITY;
            case LOAN:
                return VIEWTYPE_LOAN;
        }
        return VIEWTYPE_ADV;
    }

    @Override
    public CashAdvanceViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        CashAdvanceViewHolder viewHolder = null;
        View view;
        switch (viewType) {
            case VIEWTYPE_HEAD:
                view = layoutInflater.inflate(R.layout.layout_home_head, parent, false);
                viewHolder = new CashAdvanceViewHolder(view, viewType);
                break;
            case VIEWTYPE_NAVIGATION:
                view = layoutInflater.inflate(R.layout.layout_home_navigation, parent, false);
                viewHolder = new CashAdvanceViewHolder(view, viewType);
                break;
            case VIEWTYPE_BANNER:
                view = layoutInflater.inflate(R.layout.layout_home_banner, parent, false);
                viewHolder = new CashAdvanceViewHolder(view, viewType);
                break;
            case VIEWTYPE_IDENTITY:
                view = layoutInflater.inflate(R.layout.layout_cashadvance_identity, parent, false);
                viewHolder = new CashAdvanceViewHolder(view, viewType);
                break;
            case VIEWTYPE_LOAN:
                view = layoutInflater.inflate(R.layout.item_home_list, parent, false);
                viewHolder = new CashAdvanceViewHolder(view, viewType);
                break;
            case VIEWTYPE_ADV:
                view = layoutInflater.inflate(R.layout.item_home_list, parent, false);
                viewHolder = new CashAdvanceViewHolder(view, viewType);
                break;
        }

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(CashAdvanceViewHolder holder, int position) {
        if (holder == null) {
            return;
        }
        int itemViewType = getItemViewType(position);
        switch (itemViewType) {
            case VIEWTYPE_HEAD:
                CashHeadComponent cashCompoment = holder.cashCompoment;
                if (cashCompoment == null || homeBean == null) {
                    break;
                }
                cashCompoment.setData(homeBean.getCash_button(),homeBean.getRoll_notice());
                break;
            case VIEWTYPE_NAVIGATION:
                NavigationComponent navigationCompoment = holder.navigationCompoment;
                if (navigationCompoment == null || homeBean == null) {
                    break;
                }
                navigationCompoment.setDataList(homeBean.getBlock_list());
                break;
            case VIEWTYPE_BANNER:
                BannerComponent bannerCompoment = holder.bannerCompoment;
                if (bannerCompoment == null || bannerData == null || bannerData.size() == 0) {
                    bannerCompoment.setOwnVisible(false);
                    break;
                }
                bannerCompoment.setOwnVisible(true);
                bannerCompoment.setBannerData(bannerData);
                break;
            case VIEWTYPE_IDENTITY:
                HomeIdentityUIComponent homeIdentityUIComponent = holder.homeIdentityUIComponent;
                if (homeIdentityUIComponent == null || homeBean == null) {
                    break;
                }
                homeIdentityUIComponent.setContent(homeBean.getUser_info());
                homeIdentityUIComponent.setOnHomeLivenessListener(onHomeLivenessListener);
                break;
            case VIEWTYPE_LOAN:
                LoanAndAdvComponent itemLoanListComponent = holder.itemListComponent;
                if (itemLoanListComponent == null || homeBean == null) {
                    break;
                }
                itemLoanListComponent.setLoanData(homeBean.getLoan_commend());
                break;
            case VIEWTYPE_ADV:
                LoanAndAdvComponent itemAdvListComponent = holder.itemListComponent;
                if (itemAdvListComponent == null || homeBean == null) {
                    break;
                }
                List<HomeBean.AdvModelBean> adv_model = homeBean.getAdv_model();
                String tage = data.get(position);
                if (!tage.contains(ADV)) {
                    break;
                }
                String substring = tage.substring(ADV.length());
                try {
                    int index = Integer.parseInt(substring);
                    HomeBean.AdvModelBean advModelBean = adv_model.get(index);
                    itemAdvListComponent.setAdcData(advModelBean);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    public void setOnHomeLivenessListener(HomeIdentityUIComponent.OnHomeLivenessListener onHomeLivenessListener) {
        this.onHomeLivenessListener = onHomeLivenessListener;
    }

    @Override
    public int getItemCount() {
        return data.size();
    }
}
